package cn.com.cifi.mars.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.cifi.mars.constant.IsDeleteEnum;
import cn.com.cifi.mars.constant.VersionStatusEnum;
import cn.com.cifi.mars.constant.VersionTypeEunm;
import cn.com.cifi.mars.entity.BudgetLockRecord;
import cn.com.cifi.mars.entity.ReturnPlanVersion;
import cn.com.cifi.mars.entity.SignPlanVersion;
import cn.com.cifi.mars.mapper.mars.BudgetLockRecordMapper;
import cn.com.cifi.mars.mapper.mars.BudgetVersionIdentifyMapper;
import cn.com.cifi.mars.mapper.mars.ReturnPlanVersionMapper;
import cn.com.cifi.mars.mapper.mars.SignPlanVersionMapper;
import cn.com.cifi.mars.service.BudgetLockRecordService;
import cn.com.cifi.mars.util.StrUtils;

/**
 * <p>
 * 预算季编制锁定记录表 服务实现类
 * </p>
 *
 * @author yyd
 * @since 2021-05-10
 */
@Service
public class BudgetLockRecordServiceImpl extends ServiceImpl<BudgetLockRecordMapper, BudgetLockRecord> implements BudgetLockRecordService {

	@Autowired
	BudgetLockRecordMapper budgetLockRecordMapper;
	@Autowired
    SignPlanVersionMapper signPlanVersionMapper;
	@Autowired
	ReturnPlanVersionMapper returnPlanVersionMapper;
	@Autowired
	BudgetVersionIdentifyMapper budgetVersionIdentifyMapper;

	@Override
	public void handleProjectLockStatus(List<BudgetLockRecord> list) {
		 QueryWrapper<BudgetLockRecord> wrapper = new QueryWrapper<>();
		 List<BudgetLockRecord> dataList =  budgetLockRecordMapper.selectList(wrapper);
		 
		 List<BudgetLockRecord> updateList = new ArrayList<BudgetLockRecord>();
		 List<BudgetLockRecord> addList = new ArrayList<BudgetLockRecord>();
		 if(CollectionUtils.isEmpty(dataList)) {
			 budgetLockRecordMapper.insertBatch(list);
		 }else {
			 for(BudgetLockRecord obj : list) {
				 if(isExist(obj.getProjectId(),dataList)) {
					 updateList.add(obj);
				 }else {
					 addList.add(obj);
				 }
			 }
			 
			 if(CollectionUtils.isNotEmpty(updateList)) {
				 budgetLockRecordMapper.updateBatch(updateList);
			 }
			 
			 if(CollectionUtils.isNotEmpty(addList)) {
				 budgetLockRecordMapper.insertBatch(addList);
			 } 
			 
		 }
	}
	
	private boolean isExist(String projectId,List<BudgetLockRecord> dataList) {
		List<BudgetLockRecord> filterList = dataList.stream().filter(x -> x.getProjectId().equals(projectId)).collect(Collectors.toList());
		if(CollectionUtils.isNotEmpty(filterList)) {
			return true;
		}
		return false;
	}

	@Override
	public Integer getLockStatus(String projectId, String type) {
		BudgetLockRecord record = budgetLockRecordMapper.selectByProjectId(projectId);
		if(record != null) {
			if(type.equals("sign")) {
				return record.getSignLock();
			}else if(type.equals("return")) {
				return record.getReturnLock();
			}
		}
		return null;
	}

	@Override
	public boolean isExistFinalVersion(String stageId, String type,String versionIdentify) {
		String version = "";
		if(StrUtils.isNotEmpty(versionIdentify) && versionIdentify.indexOf("定稿版") > 0) {
			version = versionIdentify;
		}else {
			version = LocalDateTime.now().getYear() +"年度预算定稿版";
		}
		
		if(type.equals("sign")) {
			SignPlanVersion signPlanVersion = signPlanVersionMapper.selectBudgetSignPlan(stageId, VersionTypeEunm.BUDGET.getKey(),
					version,VersionStatusEnum.SYSGENERATION.getKey(), IsDeleteEnum.NO.getKey());
			
			return signPlanVersion == null ? false : true;
		}else if(type.equals("return")) {
			ReturnPlanVersion returnPlanVersion = returnPlanVersionMapper.selectBudgetReturnPlan(stageId,VersionTypeEunm.BUDGET.getKey(),
					version,VersionStatusEnum.SYSGENERATION.getKey(), IsDeleteEnum.NO.getKey());
			return returnPlanVersion == null ? false : true;
		}
		
		return false;
	}
	
}
